﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig.Controls
{
    public partial class CurrentSpeed : UserControl
    {
        public int intIndex;
        public int DeviceSelectNo;//设备选择序号
        public ParameterConfig mf;

        public CurrentSpeed(ParameterConfig mfs)
        {
            InitializeComponent();
            mf = mfs;
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="config">设备配置</param>
        /// <param name="type">设备类型</param>
        /// <param name="index">设备标识</param>
        /// <param name="intDeviceSelectNo">设备选择序号</param>
        public void InitValue(int type, int index, int intDeviceSelectNo)
        {
            gc.Text = Common.CurrentSpeedList[intDeviceSelectNo].ToString();
            DeviceSelectNo = intDeviceSelectNo;
            intIndex = index;
            this.Tag = type;
            this.TabIndex = (type + 1) * 10 + index;
            cbbType.SelectedIndex = index;
        }

        /// <summary>
        /// 设置流速仪配置
        /// </summary>
        public void SetValue()
        {
            cbbType.SelectedIndex = intIndex;
            nud1.Value = ParameterConfig.rtuConfigInfo.CurrentSpeed_Addr[intIndex];
        }

        /// <summary>
        /// 获取流速仪配置
        /// </summary>
        public void GetValue()
        {
            intIndex = cbbType.SelectedIndex;
            if (intIndex == 0)
            {
                ParameterConfig.rtuConfigInfo.CurrentSpeed_Enable |= 0x0001 << 0;
            }

            if (intIndex == 1)
            {
                ParameterConfig.rtuConfigInfo.CurrentSpeed_Enable |= 0x0001 << 1;
            }
            ParameterConfig.rtuConfigInfo.CurrentSpeedDeviceSelect[intIndex] = Convert.ToByte(DeviceSelectNo);
            ParameterConfig.rtuConfigInfo.CurrentSpeed_Addr[intIndex] = Convert.ToByte(nud1.Value);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除设备?", "删除设备", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                Control c = this.Parent;
                c.Controls.Remove(this);
                //int index = mf.IsContains(Convert.ToInt32(this.Tag), this.TabIndex);
                //if (cbbType.SelectedIndex == 0)
                //{
                //    if (index != cbbType.SelectedIndex)
                //    {
                //        mf.listDevice1.Remove(this.Tag.ToString());
                //        mf.BindFlowList1();
                //    }
                //}

                //if (cbbType.SelectedIndex == 1)
                //{
                //    if (index != cbbType.SelectedIndex)
                //    {
                //        mf.listDevice2.Remove(this.Tag.ToString());
                //        mf.BindFlowList2();
                //    }
                //}
                mf.InitFlowFs();
            }
        }

        private void btnOther_Click(object sender, EventArgs e)
        {
            try
            {
                CurrentSpeedSetting cs = new CurrentSpeedSetting(cbbType.SelectedIndex);
                cs.StartPosition = FormStartPosition.CenterParent;
                cs.ShowDialog();
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置失败！");
            }
        }

        private void btnDebug_Click(object sender, EventArgs e)
        {
            MessageInfo.SendMessage(ParameterConfig.mainFormName, ParameterConfig.debugMsg, 2, cbbType.SelectedIndex + 1);
        }

        private void cbbType_SelectedValueChanged(object sender, EventArgs e)
        {
            //mf.InitFlowmetersSource(cbbType.SelectedIndex, Convert.ToInt32(this.Tag), this.TabIndex);
            mf.InitFlowFs();
        }
    }
}
